<template>
  <div class="registerDetail">
    <div class="task_header">
      <div class="top_title">
        <div class="top_title_back" @click="onClickLeft">
          <i class="iconfont icon-back"></i>
        </div>
        <div class="top_title_val">查看详情</div>
      </div>
    </div>
    <div class="knew_content">
      <div class="titlebox">
        <div class="titlebox_name">{{ dataSource.billName }}</div>
        <div class="titlebox_info">
          <div class="txt">创建人：{{ dataSource.creatorName }}</div>
          <div class="txt">创建时间：{{ dataSource.createTime }}</div>
        </div>
      </div>
      <div class="knew_content_box">
        <div class="link_con" v-if="dataSource.type == '链接'">
          <iframe class="linkbox" :src="dataSource.content"></iframe>
        </div>
        <div class="txt_con" v-else>{{ dataSource.content }}</div>
        <div class="img_con" v-if="dataSource.type == '图片'">
          <div class="item">
            <img class="imageUrl_item" v-for="(item, index) in fileList" :key="index" :src="item" />
          </div>
        </div>
        <div class="file_con" v-if="dataSource.type == '文件'">
          <div class="carList mt" v-if="txtList && txtList.length">
            <div class="carItem" v-for="(item, index) in txtList" :key="index">
              <div class="file_box">
                <div class="file_lt">
                  <!-- <i class="iconfont icon-file"></i> -->
                  <img class="file_icon1" src="@/assets/images/excel.png" v-if="item.simpleName=='xls'||item.simpleName=='xlsx'" />
                  <img class="file_icon" src="@/assets/images/word.png" v-if="item.simpleName=='doc'||item.simpleName=='docx'" />
                  <img class="file_icon" src="@/assets/images/ppt.png" v-if="item.simpleName=='ppt'||item.simpleName=='pptx'" />
                  <img class="file_icon" src="@/assets/images/pdf1.png" v-if="item.simpleName=='pdf'" />
                  <img class="file_icon" src="@/assets/images/txt.png" v-if="item.simpleName=='txt'" />
                </div>

                <div class="txtInfo">
                  <div class="txt_name">{{ item.attachName }}</div>
                  <div class="txtInfobox">
                    <div class="createrName">{{ item.createrName }}</div>
                    <div>{{ item.attachSize }}</div>
                  </div>
                </div>
              </div>
              <i v-if="!item.downTxt" @click="downloadFile(item)" class="iconfont icon-download"></i>
              <span class="downTxt" v-else>{{ item.downTxt }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { DatetimePicker, Empty, Field, Form, Search, Uploader } from 'vant'
import { getCookies } from '../../utils/util'
import knowledgeBaseApi from '@/api/knowledgeBaseApi'
import commJs from '@/assets/js/commJs'

const base_url = process.env.BASE_API
export default {
  name: 'repairOrderDetail',
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker
  },
  data() {
    return {
      dataSource: {
        billId: '',
        billName: '',
        billNumber: '',
        content: '',
        createTime: '',
        creatorName: '',
        infoClass: '',
        lastUpdateName: '',
        type: '',
        updateTime: ''
      },
      detailId: '',
      userId: getCookies('userId'),
      fileList: [],
      txtList: []
    }
  },

  mounted() {
    if (this.$route.query) {
      if (this.$route.query.billId) {
        this.detailId = this.$route.query.billId
        this.getDetail(this.detailId)
      }
      if (this.$route.query.number) {
        this.detailId = this.$route.query.number
        this.getDetail(this.detailId)
      }
    }
  },
  methods: {
    getDetail(billId) {
      knowledgeBaseApi
        .getCustomerInfoById({
          billId,
          userId: this.userId
        })
        .then(res => {
          console.log(res)
          if (res.code == 1) {
            this.dataSource = res.data
            if (this.dataSource.type == '图片') {
              this.dataSource.attach &&
                this.dataSource.attach.forEach(item => {
                  this.fileList.push(`${base_url}/proxy/cam/file/download?attachId=${item.attachId}&attachName=${item.attachName}`)
                })
            }

            if (this.dataSource.type == '文件') {
              this.dataSource.attach &&
                this.dataSource.attach.forEach(item => {
                  this.txtList.push({
                    ...item,
                    downTxt: '',
                    progress: null,
                    url: `${base_url}/proxy/cam/file/download?attachId=${item.attachId}&attachName=${item.attachAllName}`
                  })
                })
            }
          }
        })
    },

    onClickLeft() {
      this.$router.go(-1)
    },
    // 下载文件
    downloadFile(data) {
      console.log(data)
      const formData = {
        attachId: encodeURI(data.attachId),
        attachName: data.attachName
      }
      const _this = this
      var link = document.createElement('a')

      if (data.fileName && commJs.testPlat() == 1) {
        link.href = `${base_url}/download/${data.fileName}`
        link.download = data.fileName
        link.click()
        URL.revokeObjectURL(link.href)
        return false
      }
      // if (this.lock) {
      //   return false
      // }

      data.progress = data.progress | 0
      if (data.progress == 100 && commJs.testPlat() == 0) {
        Toast('该文件已下载，请勿重复下载！')
        return false
      } else {
        data.downTxt = '已下载'
      }
      this.lock = true
      var config = {
        onUploadProgress: progressEvent => {
          var complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0
          data.progress = complete
          if (data.progress == 100) {
            this.lock = false
          }
        }
      }
      const src = `${base_url}/proxy/cam/file/download1?attachId=${formData.attachId}&attachName=${data.attachAllName}`
      var link = document.createElement('a')
      link.setAttribute('download', data.attachAllName)
      data.fileName = formData.attachName
      link.href = src
      link.click()
      URL.revokeObjectURL(link.href)
    }
  }
}
</script>

<style lang="less" scoped>
.flex {
  display: flex;
  align-items: center;
}

.registerDetail {
  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;

    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;

        .icon-right {
          margin-left: 0.2rem;
          color: #409eff;
        }
      }

      .hank {
        font-size: 0.28rem;
        color: #666;
      }
    }

    .detail_header_rt {
      justify-content: flex-start;

      .txt {
        font-size: 0.28rem;
        color: #161616;
      }

      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }

  .formData {
    background: #fff;
  }
}

.label_item {
  color: #35bc04;
  border: 1px solid #35bc04;
  border-radius: 4px;
  margin-right: 0.1rem;
  padding: 0.01rem 0.2rem;
  font-size: 0.2rem;

  &:last-child {
    margin-right: 0;
  }
}

.tabBarBox {
  margin-top: 0.2rem;
}

.tabCar {
  margin-top: 0.1rem;
}

.submitBtn {
  background: #409eff;
  color: #fff;
  width: 75%;
  margin: auto;
  text-align: center;
  padding: 0.25rem;
  border-radius: 5px;
  margin-top: 0.6rem;
  font-size: 0.3rem;
  margin-bottom: 0.4rem;
}

.noContent {
  margin-top: 0.2rem;
}
.task_header {
  background: url('~@/assets/images/title_bg.png') no-repeat center;
  font-size: 0.24rem;
  height: 50px;
  background-size: 100% 100%;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 9;

  .top_title {
    position: relative;
    width: 100%;
    height: 50px;

    .top_title_back {
      position: absolute;
      left: 10px;
      line-height: 50px;
      color: #fff;
    }

    .top_title_val {
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-weight: bold;
      font-size: 16px;
    }
    .task_transbox {
      position: absolute;
      right: 10px;
      top: 5px;
      color: #fff;
      z-index: 10;
      font-weight: 400;
      font-size: 10px;
      text-align: center;
      .task_trans {
        width: 24px;
        display: block;
        margin: auto;
      }
    }
  }

  .task_header_data {
    .task_header_date {
      margin-bottom: 10px;
      display: flex;
      align-items: center;

      .task_header_date_w {
        width: 12.5%;
      }

      .task_header_date_item {
        width: 12.5%;
        text-align: center;
        color: #fff;
        font-size: 14px;
      }
    }

    .task_header_followData {
      display: flex;
      align-items: center;
      color: #fff;
      margin-bottom: 10px;

      .task_header_followData_tt {
        width: 12.5%;
        text-align: center;
      }

      .task_header_followData_item {
        width: 12.5%;
        text-align: center;
        font-size: 12px;
        font-weight: bold;
      }
    }
  }
}
.task_detail {
  position: fixed;
  top: 50px;
  left: 0;
  width: 100%;
  z-index: 100;
  .task_detail_header {
    padding: 17px 0 10px;
    background: #fff;
    // margin-bottom: 10px;
    border-bottom: 1px solid #f1f1f1;
    position: relative;

    .title {
      margin: auto;
      display: flex;
      justify-content: center;
      align-items: center;
      .icon-edit {
        font-size: 14px;
      }
      .txt {
        text-align: center;
        //font-family: Alibaba;
        font-weight: 700;
        font-size: 16px;
        color: #333333;
        max-width: 60%;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      .status_txt {
        // background: rgba(255, 48, 33, 0.2);
        border-radius: 3px;
        padding: 3px 5px;
        font-size: 12px;
        margin-left: 5px;
      }
    }
    .name {
      display: flex;
      text-align: center;
      width: 100%;
      justify-content: center;
      margin-top: 10px;

      .txt {
        //font-family: Alibaba;
        font-weight: 400;
        font-size: 12px;
        color: #999999;
        text-align: center;
        margin-right: 10px;
      }
    }
    .status {
      //font-family: Alibaba;
      font-weight: 700;
      font-size: 12px;
      text-align: center;
      position: absolute;
      top: 16px;
      right: 10px;
      border-radius: 2px;
      padding: 5px;
      display: flex;
      align-items: center;
      .status_icon {
        width: 12px;
        margin-right: 4px;
      }
      &.status1 {
        color: #ff1100;
        background: rgba(255, 48, 33, 0.2);
      }
      &.status2 {
        color: #00b42a;
        background: #e8ffea;
      }
    }
  }
  .tabbox {
    // padding: 0 10px;
    padding-top: 15px;
    border-top: 1px solid #f7f5f5;
    margin-top: 15px;
    position: relative;
    width: 7.5rem;
    overflow: hidden;
    .tab_list {
      width: 10rem;
      overflow-x: auto;
      clear: both;
      .tab_item {
        font-weight: 400;
        font-size: 14px;
        color: #4e5969;
        text-align: center;
        background: #f7f7f7;
        border-radius: 32px;
        padding: 8px 16px;
        margin-right: 6px;
        cursor: pointer;
        float: left;
        &:last-child {
          margin-right: 0;
        }
        &.active {
          color: #165dff;
          text-align: center;
          background: #d8e3ff;
        }
      }
    }
  }
}
.knew_content {
  margin-top: 55px;
  padding: 10px;
  .titlebox {
    border-bottom: 1px solid #f8f8f8;
    padding-bottom: 10px;
    .titlebox_name {
      font-size: 16px;
      color: #333;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .titlebox_info {
      display: flex;
      justify-content: space-between;
      .txt {
        font-size: 12px;
        color: #666;
      }
    }
  }
  .knew_content_box {
    padding-top: 10px;
    margin-bottom: 20px;
    width: 100%;
    .txt_con {
      font-size: 14px;
      width: 100%;
      overflow: hidden;
      clear: both;
    }
    .img_con {
      width: 100%;
      margin: auto;
      .item {
        width: 100%;
      }
      .imageUrl_item {
        width: 100%;
        display: block;
        margin: auto;
        margin-top: 10px;
      }
    }
    .file_con {
      width: 100%;
    }
    .link_con {
      margin-top: 10px;
      .linkbox {
        width: 100%;
        height: 100vh;
        overflow: auto;
      }
    }
  }
}
.downTxt {
  font-size: 12px;
  color: red;
}

.mb80 {
  margin-bottom: 80px;
}
.mt0 {
  margin-top: 0px;
}
.mt175 {
  margin-top: 175px;
}
.mt160 {
  margin-top: 165px;
}
.mb60 {
  margin-bottom: 60px;
}
.detailbg {
  background: #f3f3f3;
}
.w100 {
  width: 100% !important;
}

.mt165 {
  margin-top: 165px;
}

.carList {
  background: #fff;
  margin-top: 10px;
  width: 100%;
  margin: auto;
  overflow: hidden;

  .carItem {
    background: #fff;
    padding: 10px 12px;
    border: 1px solid #dfdfdf;
    justify-content: space-between;
    overflow: hidden;
    border-radius: 3px;
    margin-top: 10px;
    display: flex;
    .file_lt {
      font-size: 0.26rem;
      color: #161616;
      .file_icon {
        display: block;
        width: 25px;
      }
      .file_icon1 {
        display: block;
        width: 30px;
        // position: relative;
        // left: -2px;
        margin-left: -2px;
      }
    }

    .icon-file {
      color: #999;
      margin-right: 0.1rem;
      position: relative;
      top: 1px;
    }
    .icon-download {
      font-size: 0.5rem;
      color: #409eff;
      position: relative;
      top: 8px;
    }
    .vin {
      color: #666;
      font-size: 0.28rem;
    }
  }
  .file_box {
    display: flex;
    align-items: center;
  }
  .txtInfo {
    font-size: 12px;
    margin-left: 10px;
    .txt_name {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .txtInfobox {
      font-size: 12px;
      display: flex;
    }
    .createrName {
      border-right: 1px solid #ccc;
      margin-right: 5px;
      padding-right: 5px;
    }
  }
}
</style>
